import { ref } from 'vue'
import { defineStore } from 'pinia'
import { getLoginUsingGet } from '@/api/userController.ts'

/**
 * 存储登录用户信息的Store
 */
export const useLoginUserStore = defineStore('loginUser', () => {
  const loginUser = ref<API.UserLoginVo>({
    userName: '未登录',
  })

  /**
   * 设置登录用户
   * @param newloginUser
   */
  function setLoginUser(newloginUser: any) {
    loginUser.value = newloginUser
  }

  async function fetchLoginUser() {
    const res = await getLoginUsingGet()
    if (res.data.code === 0 && res.data.data) {
      loginUser.value = res.data.data
    }
  }

  return { loginUser, fetchLoginUser, setLoginUser }
})
